//
//  TabBar.swift
//  WeiBo_NB
//
//  Created by apple on 15/3/5.
//  Copyright (c) 2015年 Ai. All rights reserved.
//

import UIKit

class TabBar: UITabBar {

     /// 微博按钮回调闭包
    var composeBtnClicked:(()->())?
    
    /// 添加按钮“+”
    override func awakeFromNib() {
     self.addSubview(addBtn!)
    }
    
    /// 设置按钮大小
    override func layoutSubviews() {
        super.layoutSubviews()
        setFrame()
    }
    
    /// 设置按钮大小
    let count = 5
    func setFrame(){
        var w = self.bounds.size.width / 5
        var h = self.bounds.size.height
        var index = 0
     
        for view in self.subviews as! [UIView] {
            // UITabBarButton
            if view is UIControl && !(view is UIButton){
                let rect = CGRectMake(CGFloat(index) * w, 0, w, h)
                view.frame = rect
                index++
                
                if index == 2{
                    index++
                }
            }
        }
        addBtn!.frame = CGRectMake(2 * w, 0, w, h)
    }
    /// +按钮懒加载
    lazy var addBtn:UIButton?={
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "ClickBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    ///  点击了“+”button
    func ClickBtn(){
        if composeBtnClicked != nil{
            composeBtnClicked!()
        }

        
        
    }
}
